<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AlreadyRegisteredPlugn
 *
 * @author JeyDotC
 * @export Plugins
 */
class AlreadyRegisteredPlugn extends UnderscorePlugin implements BeforeTask {

    public function beforeTask($task) {
        $result = true;
        if ($this->getModule()->getName() == "MatriculaAcademica") {
            $user = AcawebUtils::GetUser();
            if (!$user->isAnonimous() && in_array("ROLE_STUDENT", $user->getPermissions())) {
                if ($this->isAlreadyRegistered($user->getName())) {
                    $result = false;
                    $routing = AcawebUtils::GetRouting();
                    $routing->redirect($routing->appAction("Default", "FrontPage"));
                }
            }
        }
        return $result;
    }

    private function isAlreadyRegistered($codigoEstudiante) {
        $alreadyRegisterd = false;
        
        $cursosMatriculados = $this->getModule()->model->registeredCourse->GetByEstudianteAndPeriodoAcademico($codigoEstudiante, AcawebUtils::GetPeriodoAcademicoMatriculas());
        
        if (count($cursosMatriculados) > 0) {
            $alreadyRegisterd = true;
        }
        return $alreadyRegisterd;
    }

}

?>
